<?php

namespace MemLib;

/**
 * 分页对象
 *
 * @version 1.0
 * @author 张云江
 */
class PageInfo
{
    /**
     * 当前页索引
     * @var int
     */
    public $pageindex;
    /**
     * 当前页尺寸
     * @var int
     */
    public $pagesize;
    /**
     * 数据数量
     * @var double
     */
    public $count;
    /**
     * 构建分页对象
     * @param mixed $pageindex 当前页索引,第一页:0,第二页:1
     * @param mixed $pagesize 当前分页尺寸
     */
    public function __construct($pageindex, $pagesize)
    {
        $this->pagesize = (int)$pagesize;
        $this->pageindex = (int)$pageindex;
        if ($this->pageindex < 0)
            $this->pageindex = 0;
        $this->count = 0;
    }
    /**
     * 获取总页数
     * @return int
     */
    public function getPageCount()
    {
        $count_s = (int)($this->count % $this->pagesize);
        $size = (int)($this->count / $this->pagesize);
        if ($count_s > 0)
            $size++;
        return $size;
    }
    /**
     * 显示分页页数集合,例如:第一组1,2,3,4,5,第二组6,7,8,9,10
     * @param int $showpagecount 多少页为一组
     * @return array
     */
    public function getGroupPage($showpagecount)
    {
        $showpagecount = (int)$showpagecount;
        $pagecount = $this->getPageCount();
        $pagearray = array();
        $group = (int)($this->pageindex / $showpagecount);
        $showlength = $showpagecount * $group;
        for ($i = $showlength; $i < ($showlength + $showpagecount); $i++) {
            if ($i >= $pagecount)
                break;
            $pagearray[] = $i;
        }
        return $pagearray;
    }
    /**
     * 显示上一个分组最后一页,如果无法显示返回-1
     * @param mixed $showpagecount 每组分页数量
     * @return int
     */
    public function getGroupPageIndexWithPre($showpagecount)
    {
        $nowarray = $this->getGroupPage($showpagecount);
        if (count($nowarray) == 0)
            return -1;
        $firstpage = (int)$nowarray[0];
        $firstpage--;
        if ($firstpage < 0)
            return -1;
        return $firstpage;
    }
    /**
     * 显示下一个分组的第一页,如果无法显示返回-1
     * @param mixed $showpagecount 每组分页数量
     * @return int
     */
    public function getGroupPageIndexWithNext($showpagecount)
    {
        $nowarray = $this->getGroupPage($showpagecount);
        $count = count($nowarray);
        if ($count == 0)
            return -1;
        //获取当前分组的最后一页索引
        $lastpage = (int)$nowarray[$count - 1];
        //尝试检索下个分组
        $lastpage++;
        $nowpageindex = $this->pageindex;
        //因为getGroupPage要通过pageindex进行检索
        $this->pageindex = $lastpage;
        $nowarray = $this->getGroupPage($showpagecount);
        //原始索引交还给pageindex,至此$this->pageindex已经没有作用
        $this->pageindex = $nowpageindex;
        $nextcount = count($nowarray);
        if ($nextcount == 0)
            return -1;
        //如果$lastpage大于$nowarray第一个元素即没有下个分组,因为这时$nowarray是尝试后得到的下个分组
        if ($lastpage > (int)$nowarray[$nextcount - 1])
            return -1;
        return $nowarray[0];
    }
    /**
     * 获取limit指令索引
     * @return double|int
     */
    public function getLimitIndex()
    {
        return $this->pageindex * $this->pagesize;
    }
}
